1
/****************************** Module Header ******************************\
2 * Module Name: CSASPNETMenu.aspx.cs
3 * Project: CSASPNETMenu
4 * Copyright (c) Microsoft Corporation.
6 * The CSASPNETMenu sample demonstrates how to bind a ASP.NET Menu Control
9 * All other rights reserved.
12 * * 8/27/2009 11:00 AM Zong-Qing Li Created
13 \***************************************************************************/
15 using System
.Collections
.Generic
;
19 using System
.Web
.UI
.WebControls
;
22 namespace CSASPNETMenu
24 public partial class CSASPNETMenu
: System
.Web
.UI
.Page
26 protected void Page_Load(object sender
, EventArgs e
)
34 public void GenerateMenuItem()
36 // Get the data from database.
37 DataSet ds
= GetData();
39 foreach (DataRow mainRow
in ds
.Tables
[0].Rows
)
41 // Load the records from the main table to the menu control.
42 MenuItem masterItem
= new MenuItem(mainRow
["mainName"].ToString());
43 masterItem
.NavigateUrl
= mainRow
["mainUrl"].ToString();
44 Menu1
.Items
.Add(masterItem
);
46 foreach (DataRow childRow
in mainRow
.GetChildRows("Child"))
48 // According to the relation of the main table and the child table, load the data from the child table.
49 MenuItem childItem
= new MenuItem((string)childRow
["childName"]);
50 childItem
.NavigateUrl
= childRow
["childUrl"].ToString();
51 masterItem
.ChildItems
.Add(childItem
);
56 public DataSet
GetData()
58 // In order to test, we use the memory tables as the datasource.
59 DataTable mainTB
= new DataTable();
60 DataColumn mainIdCol
= new DataColumn("mainId");
61 DataColumn mainNameCol
= new DataColumn("mainName");
62 DataColumn mainUrlCol
= new DataColumn("mainUrl");
63 mainTB
.Columns
.Add(mainIdCol
);
64 mainTB
.Columns
.Add(mainNameCol
);
65 mainTB
.Columns
.Add(mainUrlCol
);
67 DataTable childTB
= new DataTable();
68 DataColumn childIdCol
= new DataColumn("childId");
69 DataColumn childNameCol
= new DataColumn("childName");
71 // The MainId column of the child table is the foreign key to the main table.
72 DataColumn childMainIdCol
= new DataColumn("MainId");
73 DataColumn childUrlCol
= new DataColumn("childUrl");
75 childTB
.Columns
.Add(childIdCol
);
76 childTB
.Columns
.Add(childNameCol
);
77 childTB
.Columns
.Add(childMainIdCol
);
78 childTB
.Columns
.Add(childUrlCol
);
81 // Insert some test records to the main table.
82 DataRow dr
= mainTB
.NewRow();
87 DataRow dr1
= mainTB
.NewRow();
92 DataRow dr2
= mainTB
.NewRow();
97 DataRow dr3
= mainTB
.NewRow();
100 dr3
[2] = "test.aspx";
101 mainTB
.Rows
.Add(dr3
);
104 // Insert some test records to the child table
105 DataRow dr5
= childTB
.NewRow();
109 dr5
[3] = "test.aspx";
110 childTB
.Rows
.Add(dr5
);
111 DataRow dr6
= childTB
.NewRow();
113 dr6
[1] = "SQL Server";
115 dr6
[3] = "test.aspx";
116 childTB
.Rows
.Add(dr6
);
117 DataRow dr7
= childTB
.NewRow();
119 dr7
[1] = "JavaScript";
121 dr7
[3] = "test.aspx";
122 childTB
.Rows
.Add(dr7
);
124 // Use the DataSet to contain that two tables.
125 DataSet ds
= new DataSet();
126 ds
.Tables
.Add(mainTB
);
127 ds
.Tables
.Add(childTB
);
129 // Build the relation between the main table and the child table.
130 ds
.Relations
.Add("Child", ds
.Tables
[0].Columns
["mainId"], ds
.Tables
[1].Columns
["MainId"]);